﻿using ERPWinFormWork.Sales.DTO;
using ERPWinFormWork.Sales.ErrorCode;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ERPWinFormWork.Sales
{
    public partial class ProductAddForm : Form
    {
        private ApiClient _apiClient;
        public List<ProductModel> SelectedProducts { get; private set; }
        public ProductAddForm()
        {
            InitializeComponent();
            _apiClient = new ApiClient(AppConfig.ApiBaseUrl);
            GetCustomerSelect();

        }


        private async void GetCustomerSelect()
        {
            var response = await _apiClient.GetAsync<ApiResponse<List<ProductModel>>>(
                    endpoint: "Sales/ProductGet"
                );
            if (response != null && response.code == 200)
            {

                dgvproduct.DataSource = response.data;
                DataGridViewCheckBoxColumn checkBoxColumn = new DataGridViewCheckBoxColumn();
                checkBoxColumn.HeaderText = ""; // 可设置列标题，这里设为空
                checkBoxColumn.Name = "Selected"; // 列名，用于后续获取选中状态
                dgvproduct.Columns.Insert(0, checkBoxColumn); // 插入到第一列，也可根据需求调整位置
            }
        }

        private void btnNotarize_Click(object sender, EventArgs e)
        {
             SelectedProducts  = new List<ProductModel>();
            // 遍历 DataGridView 的行
            foreach (DataGridViewRow row in dgvproduct.Rows)
            {
                // 获取复选框单元格的值，判断是否选中
                DataGridViewCheckBoxCell checkBoxCell = (DataGridViewCheckBoxCell)row.Cells["Selected"];
                if (checkBoxCell != null)
                {
                    // 获取该行对应的数据模型（假设 ProductModel 是绑定的实体类）
                    ProductModel product = (ProductModel)row.DataBoundItem;
                    SelectedProducts.Add(product);
                }
            }

            // 此时 selectedProducts 就是选中行对应的数据集合，可根据需求进一步处理
            if (SelectedProducts.Count > 0)
            {

                this.DialogResult = DialogResult.OK; // 设置对话框结果，方便上一级判断
                this.Close();

            }
            else
            {
                MessageBox.Show("请选择至少一条数据");
            }
        }
    }
}
